package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.enums.MessageType;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.OtherRecord;
import cn.fintecher.pangolin.entity.elastic.WorkOrderModel;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.OtherRecordElastic;
import cn.fintecher.pangolin.service.dataimp.service.BaseService;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:37 2019/10/14
 */

@Service("ConfirmWorkOrderTask")
public class ConfirmWorkOrderTask {
    private static final Logger log = LoggerFactory.getLogger(ConfirmWorkOrderTask.class);

    @Autowired
    private OtherRecordElastic otherRecordElastic;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    @Autowired
    private ConfirmWorkOrderFuture confirmWorkOrderFuture;

    @Autowired
    private BaseService baseService;

    /**
     * 工单导入确认
     */
    @Async
    @Transactional
    public void confirmWorkOrder(List<WorkOrderModel> models, UserModel userModel, DataImportRecord record) {
        //创建任务集合
        List<CompletableFuture<List<OtherRecord>>> list = new ArrayList<>();
        //分页处理信息
        int pageSize = 2000;
        int pageNumber = (int) Math.ceil((double) models.size() / (double) pageSize);
        for (int i = 0; i < pageNumber; i++) {
            List<WorkOrderModel> workOrderModels = models.stream().skip(i * pageSize).limit(pageSize).collect(Collectors.toList());
            CompletableFuture<List<OtherRecord>> task = confirmWorkOrderFuture.processWorkOrder(workOrderModels, userModel);
            list.add(task);
        }
        List<OtherRecord> records = new ArrayList<>();
        try {
            for (CompletableFuture<List<OtherRecord>> future : list) {
                List<OtherRecord> otherRecords = future.get();
                records.addAll(otherRecords);
            }
            if (!records.isEmpty()) {
                RepositoryUtil.saveData(otherRecordElastic, records);
            }
            //更新导入结果状态
            record.setStatus(ImportDataExcelStatus.CONFIRMED);
            record.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(record);
        } catch (InterruptedException | ExecutionException e) {
            log.error(e.getMessage(), e);
            record.setStatus(ImportDataExcelStatus.UN_CONFIRMED);
            record.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
            dataImportRecordElastic.save(record);
        } finally {
            baseService.sendMsg(MessageType.IMPORT_WORKER_CONFIRMED, userModel);
        }
    }
}